#!/bin/sh
set -e

ucf_delete_unchanged() {
  # pdns-server no longer Depends: ucf, so only do this if ucf is installed.
  if which ucf >/dev/null; then
    if ucfq --with-colons "$1" | grep -q ':pdns-server:Yes:No'; then
      # file exists, not changed; delete it
      echo "Removing unchanged configuration file $1"
      rm -f "$1"
    fi
    ucf --purge "$1"
    ucfr --purge pdns-server "$1"
  else
    echo "E: ucf not installed, can not remove unchanged configuration file $1."
  fi
}

purge_ucf_registry() {
  pkgname="$1"
  # Remove files previously registered with ucf (but keep the files).
  # Newer versions no longer use ucf to manage conffiles.
  # We stopped depending on ucf, so avoid calling ucf when it is not present.
  if command -v ucfq >/dev/null; then
    for file in $(ucfq --with-colons "$pkgname" | cut --delimiter=: --fields=1); do
      ucf --purge ${file}
      ucfr --purge $pkgname ${file}
    done
  fi
}

backup_conffile() {
  if [ -e "$1" ]; then
    echo "Moving configuration file $1 to $1.dpkg-bak"
    mv -f "$1" "$1".dpkg-bak
  fi
}

case "$1" in
  install|upgrade)
    # for upgrades from before 4.0.0-2, drop empty file created by previous postinst.
    if [ -e /etc/powerdns/bindbackend.conf ] && echo "d41d8cd98f00b204e9800998ecf8427e /etc/powerdns/bindbackend.conf" | md5sum --check --status; then
      rm -f /etc/powerdns/bindbackend.conf
    fi

    # 4.0.0-2 moved pdns-server' configuration files back under dpkg conffile control (from ucf),
    # and bindbackend was split into its own package.
    # the upgrade code in 4.0.0-2 did not delete unchanged files.
    if [ ! -z "$2" ] && dpkg --compare-versions "$2" le "4.0.0-3~"; then
      # we want to preserve the following if they were changed.
      ucf_delete_unchanged "/etc/default/pdns"
      ucf_delete_unchanged "/etc/powerdns/pdns.conf"
      ucf_delete_unchanged "/etc/powerdns/pdns.d/pdns.local.conf"
      # pdns.simplebind.conf is no longer useful with just pdns-server,
      # pdns-backend-bind will install bind.conf instead.
      ucf_delete_unchanged "/etc/powerdns/pdns.d/pdns.simplebind.conf"
      backup_conffile "/etc/powerdns/pdns.d/pdns.simplebind.conf"
    fi

    # Remove files no longer in the package (and unused)
    rm -f /etc/resolvconf/update.d/pdns
    rm -f /etc/insserv.d/pdns-server

    purge_ucf_registry "pdns-server"
  ;;
esac

#DEBHELPER#
